// pages/myself/member/Member.js
let network = null;
let utils = null;
let api = null;
var Zan = require('../../../dist/index');
let WxNotificationCenter = require('../../../utils/WxNotificationCenter.js');
const app = getApp();

Page(Object.assign({}, Zan.TopTips, Zan.Toast,{

  /**
   * 页面的初始数据
   */
  data: {
    tenantMember: null,
    index: 0,
    enddate:'',
    menberon:false,
    memType:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    if (app.globalData.userInfo.typeId){
      this.setData({
        menberon: true,
        memType: app.globalData.userInfo.memType
      })
    }
    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    network._post_json(this, api.getTenantMemberType, {}, tenantMemberRuleSuccess);
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  selectTop: function (e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    for (let i = 0; i < this.data.tenantMember.length; i++) {
      if (this.data.tenantMember[i].id == id) {
        this.data.tenantMember[i].isSelect = true;
      } else {
        this.data.tenantMember[i].isSelect = false;
      }
    }
    this.setData({
      tenantMember: this.data.tenantMember,
      index: index
    })
  },
  pay:function(e){
    let id = e.currentTarget.dataset.id;
    network._post_json(this, api.openMembercard, { id: id }, opensuccess);
  }
}))

var tenantMemberRuleSuccess = function (that, res) {
  if (res.data.data) {
    let temp = [];
    let data = res.data.data;
    for (let i = 0; i < data.length; i++) {
      if (data[i].memType != '普通用户') {
        temp.push(data[i]);
      }
    }
    for (let i = 0; i < temp.length; i++) {
      if (i == 0) {
        temp[i].isSelect = true;
      } else {
        temp[i].isSelect = false;
      }
    }
    
    // for (let j = 0; j < res.data.data.length; j++) {
    //   if (res.data.data[j].endTime != 0) {
    //     res.data.data[j].endTime = dateformat(res.data.data[j].endTime)
    //   }
    // }
    
    that.setData({
      tenantMember: temp
    })
  }
}
var opensuccess = function(that,res){
  if(res.data.data){
    if (res.data.data.isfree == 1){
      wx.requestPayment({
        timeStamp: res.data.data.timeStamp,
        nonceStr: res.data.data.nonceStr,
        package: res.data.data.package,
        signType: res.data.data.signType,
        paySign: res.data.data.paySign,
        success: function (res) {
          that.showZanToast('开通成功！');

          setTimeout(function () {
            WxNotificationCenter.postNotificationName('MemberRefreshNotification');
            wx.redirectTo({
              url: '/pages/myself/member/MyMember',
            })
          }, 2000);
          console.log(res)
        },
        fail: function (res) {
          console.log(res)
        },
      });
    }else{
      that.showZanToast('免费开通成功！');
      setTimeout(function () {
        WxNotificationCenter.postNotificationName('MemberRefreshNotification');
        wx.redirectTo({
          url: '/pages/myself/member/MyMember',
        })
      }, 2000);
    }    
  }
}